package com.an_yang_gang_yie.an_yang_gang_yie.controller;

import com.an_yang_gang_yie.an_yang_gang_yie.entity.AjaxResponseBody;
import com.an_yang_gang_yie.an_yang_gang_yie.enum_util.ResponseBodyEnum;
import com.an_yang_gang_yie.an_yang_gang_yie.system.entity.AdminUser;
import com.an_yang_gang_yie.an_yang_gang_yie.system.mapper.AdminUserMapper;
import com.an_yang_gang_yie.an_yang_gang_yie.utils.Utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@Api(value = "登录模块",tags = {"登录模块"})
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private AdminUserMapper adminUserMapper;
       @PostMapping("/admin")
       @ResponseBody//将java对象转为json格式的数据。
       @ApiOperation("管理员登录接口")
      //将java对象转为json格式的数据。
       public AjaxResponseBody login(@RequestBody AdminUser adminUser){
           String token = null;//token
           /**
            * 校验数据是否为空
            */
           if(adminUser.getUsername()==null || adminUser.getPassword()==null)return  new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_USER_LOGIN_ERROR);
           QueryWrapper<AdminUser> adminUserQueryWrapper = new QueryWrapper<>();
           adminUserQueryWrapper.eq("username",adminUser.getUsername())
                   .or()
           .eq("email",adminUser.getUsername())
           .eq("password",adminUser.getPassword());
           System.out.println(adminUser.toString());
           AdminUser findData = adminUserMapper.selectOne(adminUserQueryWrapper);
           /**
            * 用户存在 与不存在逻辑
            */
           if(findData!=null){
               token = Utils.getToken(findData.getUsername());
               return new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_ENUM_OK,findData,token);
           }else {
               //账户密码错误
               System.out.println(findData);
               return new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_USER_LOGIN_ERROR);
           }


       }
}
